<?php
    header('Access-Control-Allow-Origin: *');
    header('Content-Type:text/html;charset=utf-8');
    $con = mysqli_connect("127.0.0.1", "root", "root", "mydb");
    if (!$con) {
        die('连接失败: ' . mysqli_error($con));
    }
    // echo '连接成功<br />';
    $uId = $_POST['uId'];
    $uName = $_POST['uName'];
    $uSex = $_POST['uSex'];
    $classId = $_POST['classId'];
    $uPwd = $_POST['uPwd'];
    $status = $_POST['status'];
    $email = $_POST['email'];
    if($status == 'Student'){
        $sql = "SELECT * FROM stu_info WHERE stu_id = '$uId';";
        $res = mysqli_query($con,$sql);
        if(mysqli_affected_rows($con) == 0){
            $sql1 = "SELECT * FROM class_info WHERE c_id = '$classId';";
            $res1 = mysqli_query($con,$sql1);
            if(mysqli_affected_rows($con) > 0){
                $sql2 = "INSERT INTO stu_info (stu_id,stu_pwd,stu_name,stu_sex,class_id) VALUES('$uId','$uPwd','$uName','$uSex','$classId');";
                $res2 = mysqli_query($con,$sql2);
                if(mysqli_affected_rows($con) > 0){
                    echo 'ok';
                }else{
                    echo 'fail';
                }
            }else{
                echo 'noclass';
            }
        }else{
            echo 'repate';
        }
    }else if ($status == 'Teacher'){
        $sql = "SELECT * FROM tch_info WHERE t_id = '$uId';";
        $res = mysqli_query($con,$sql);
        if(mysqli_affected_rows($con) == 0){
            $sql2 = "INSERT INTO tch_info (t_id,t_pwd,t_name,t_sex,t_email) VALUES('$uId','$uPwd','$uName','$uSex','$email');";
            $res2 = mysqli_query($con,$sql2);
            if(mysqli_affected_rows($con) > 0){
                echo 'ok';
            }else{
                echo 'fail';
            }
        }else{
            echo 'repate';
        }
    }
    mysqli_close($con);
?>